package com.readbook.action;


import com.readbook.service.WbookdetailService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/wbookdetail")
public class wbookdetail {
    @Resource
    private WbookdetailService wbookdetailService;


    /**
     * @author 牟明艳
     * @comments 根据书籍ID查询书的详情
     * @time 2019年8月24日23:24:54
     */
    @RequestMapping("getBookDetail.html")
    @ResponseBody
    public Map<String, Object> getBookDetail(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        String usid = request.getParameter("usid");
        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("bid", bid);
        getm.put("usid", usid);
        Map<String, Object> detail = new HashMap<String, Object>();
        detail = wbookdetailService.getBookDetail(getm);
        int totalpage = 0;
        if (detail.get("TOTALPAGE") != null) {
            totalpage = Integer.parseInt(detail.get("TOTALPAGE").toString());
        }
        int minnum = 0;
        if (detail.get("MINNUM") != null) {
            minnum = Integer.parseInt(detail.get("MINNUM").toString());
        }
        if (minnum == 0) {
            detail.put("ygts", 0);
        } else {
            double c = totalpage / minnum;
            detail.put("ygts", (int) Math.ceil(c));
        }
        return detail;
    }


    /**
     * @author 牟明艳
     * @comments 根据书籍ID查询心得体会
     * @time 2019年8月25日00:09:18
     */
    @RequestMapping("getReadlearnBybid.html")
    @ResponseBody
    public Map<String, Object> getReadlearnBybid(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("bid", bid);

        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int start = page * limit - limit + 1;
        int end = page * limit;
        getm.put("start", start);
        getm.put("end", end);
        String usid = request.getParameter("usid");
        getm.put("usid", usid);

        List<Map<String, Object>> xdth = new ArrayList<>();
        xdth = wbookdetailService.getReadlearnBybid(getm);
        int allitems = wbookdetailService.countReadlearnBybid(getm);
        double c = (double) allitems / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> setm = new HashMap<String, Object>();
        setm.put("xdth", xdth);
        setm.put("totalpage", totalpage);

        return setm;
    }


    /**
     * @author 牟明艳
     * @comments 根据书籍ID查询讨论
     * @time 2019年8月25日00:48:53
     */
    @RequestMapping("getDiscussBybid.html")
    @ResponseBody
    public Map<String, Object> getDiscussBybid(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        String usid = request.getParameter("usid");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int start = page * limit - limit + 1;
        int end = page * limit;

        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("bid", bid);
        getm.put("usid", usid);
        getm.put("start", start);
        getm.put("end", end);
        List<Map<String, Object>> discuss = new ArrayList<>();
        discuss = wbookdetailService.getDiscussBybid(getm);
        int allitems = wbookdetailService.countDiscussBybid(getm);
        Map<String, Object> setm = new HashMap<String, Object>();
        setm.put("discuss", discuss);
        double c = (double) allitems / limit;
        int totalpage = (int) Math.ceil(c);
        setm.put("totalpage", totalpage);
        return setm;
    }


    /**
     * @author 牟明艳
     * @comments 根据书籍ID获取积分排行
     * @time 2019年8月25日01:07:05
     */
    @RequestMapping("getReadJfPh.html")
    @ResponseBody
    public Map<String, Object> getReadJfPh(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        String nj = request.getParameter("nj");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int start = page * limit - limit + 1;
        int end = page * limit;
        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("bid", bid);
        getm.put("nj", nj);
        getm.put("start", start);
        getm.put("end", end);
        List<Map<String, Object>> lisjf = new ArrayList<>();
        lisjf = wbookdetailService.getReadJfPh(getm);
        int allitems = wbookdetailService.countReadJfPh(getm);
        double c = (double) allitems / limit;
        int totalpage = (int) Math.ceil(c);
        Map<String, Object> setm = new HashMap<String, Object>();
        setm.put("jfph", lisjf);
        setm.put("totalpage", totalpage);
        return setm;
    }


    /**
     * @author 牟明艳
     * @comments 查询是否收藏  是否加入书架
     * @time 2019年8月26日22:44:07
     */
    @RequestMapping("getSfscJrsj.html")
    @ResponseBody
    public Map<String, Object> getSfscJrsj(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        String usid = request.getParameter("usid");
        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("bid", bid);
        getm.put("usid", usid);
        Map<String, Object> setm = new HashMap<String, Object>();
        setm = wbookdetailService.getSfscJrsj(getm);
        return setm;
    }


    /**
     * @author 牟明艳
     * @comments 收藏书籍
     * @time 2019年8月26日22:44:07
     */
    @RequestMapping("collectionbook.html")
    @ResponseBody
    public boolean collectionbook(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        String usid = request.getParameter("usid");
        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("bid", bid);
        getm.put("usid", usid);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        getm.put("addtime", df.format(new Date()));
        boolean result = wbookdetailService.collectionbook(getm);
        return result;
    }


    /**
     * @author 牟明艳
     * @comments 取消收藏书籍
     * @time 2019年9月9日17:58:34
     */
    @RequestMapping("cancelcollectionbook.html")
    @ResponseBody
    public boolean cancelcollectionbook(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        String usid = request.getParameter("usid");
        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("bid", bid);
        getm.put("usid", usid);
        boolean result = wbookdetailService.cancelcollectionbook(getm);
        return result;
    }


    /**
     * @author 牟明艳
     * @comments 加入书架
     * @time 2019年8月26日22:44:07
     */
    @RequestMapping("insertbookplan.html")
    @ResponseBody
    public boolean insertbookplan(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        String usid = request.getParameter("usid");
        String minpage = request.getParameter("minpage");
        String username = request.getParameter("username");

        Map<String, Object> getm = new HashMap<String, Object>();
        getm.put("bid", bid);
        getm.put("usid", usid);
        getm.put("minpage", minpage);
        getm.put("username", username);
        String planid = UUID.randomUUID().toString().replaceAll("-", "");
        getm.put("planid", planid);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        getm.put("addtime", df.format(new Date()));
        boolean result = wbookdetailService.insertbookplan(getm);
        return result;
    }


}
